package rsvp.original_translation_combine;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import junit.framework.Assert;

import org.junit.Test;

import rsvp.util.FileUtility;

/**
 * get a result string that combines translated text with original text
 *  
 * @author ksz
 *
 */
public class Test_CombineOriginalWithTranslation
{		
	/**
	 * @throws IOException
	 */
	@Test public void test_combineOriginalWithTranslation_bySentence() throws IOException
	{
		final File fInO = Sentence.TEST_PATH_ORIGINAL_FILE;
		final File fInT = Sentence.TEST_PATH_TRANSLATION_FILE;
		final File fOutCombined = Sentence.TEST_PATH_COMBINED_FILE;

		combineOriginalWithTranslation_bySentence(fInO, fInT, fOutCombined);
	}

	/**
	 * @throws IOException
	 */
	@Test public void test_combineOriginalWithTranslation_byParagraph() throws IOException
	{
		final File fInO = Sentence.TEST_PATH_ORIGINAL_FILE;		
		final File fInT = Sentence.TEST_PATH_TRANSLATION_FILE;
		final File fOutCombined = Sentence.TEST_PATH_COMBINED_FILE;

		merge_andAlternateParagraphs(fInO, fInT, fOutCombined);
	}

	/**
	 * @param fInO
	 * @param fInT
	 * @param fOutC
	 * @throws IOException
	 */
	public static void merge_andAlternateParagraphs(File fInO, File fInT, File fOutC)
																	throws IOException
	{
//		System.out.println("merge_andAlternateParagraphs(File fInO = " + fInO.getAbsolutePath());
		String strOriginal = FileUtility.readFile(fInO);
//		FileUtility.writeToFile(new File(fOutC.getAbsolutePath() + "_testO"), strOriginal);
		String strTranslation = FileUtility.readFile(fInT);
//		FileUtility.writeToFile(new File(fOutC.getAbsolutePath() + "_testT"), strTranslation);
//		System.out.println(strTranslation.substring(0, 9*9*9));
		
		
		ArrayList<Sentence> arSentenceOriginal = Sentence.extractSentences(strOriginal, false);
		ArrayList<Sentence> arSentenceTranslation = Sentence.extractSentences(strTranslation, true);
		
		int iOriginalCursor = 0;
		int iTranslationCursor = 0;
		
		ArrayList<Sentence> aCombined = new ArrayList<Sentence>();
		
		for(iOriginalCursor = 0; 
				iOriginalCursor < arSentenceOriginal.size() 
				&& iTranslationCursor < arSentenceTranslation.size(); 
				) {
			iOriginalCursor = Test_CombineOriginalWithTranslation.addRemainingSentencesTillParagraph(aCombined, arSentenceOriginal, iOriginalCursor);
			iTranslationCursor = Test_CombineOriginalWithTranslation.addRemainingSentencesTillParagraph(aCombined, arSentenceTranslation, iTranslationCursor);
		}
		
		Sentence.outputResultFileParagraphs(aCombined, fOutC);
		
	
		Sentence senO = arSentenceOriginal.get(arSentenceOriginal.size() - 1);
		Sentence senT = arSentenceTranslation.get(arSentenceTranslation.size() -1);
	
		Assert.assertEquals("Number of paragraphs should be equal.", 
		                    senO.getLocation_paragraphNumber(), 
		                    senT.getLocation_paragraphNumber());
	}
	
	/**
	 * @param fInO
	 * @param fInT
	 * @param fOutCombined
	 * @throws IOException
	 */
	private void combineOriginalWithTranslation_bySentence(
															final File fInO,
															final File fInT,
															final File fOutCombined)
																					throws IOException
	{
		String strOriginal = FileUtility.readFile(fInO);
		String strTranslation = FileUtility.readFile(fInT);

		ArrayList<Sentence> arSentenceOriginal = Sentence.extractSentences(strOriginal, false);
		ArrayList<Sentence> arSentenceTranslation = Sentence.extractSentences(strTranslation, true);
		
		int iOriginalCursor = 0;
		int iTranslationCursor = 0;
		
		ArrayList<Sentence> aCombined = new ArrayList<Sentence>();
		
		for(iOriginalCursor = 0; 
				iOriginalCursor < arSentenceOriginal.size() 
				&& iTranslationCursor < arSentenceTranslation.size(); 
				) {
			Sentence senO = arSentenceOriginal.get(iOriginalCursor);
			Sentence senT = arSentenceTranslation.get(iTranslationCursor);
			
			/*-
			 * *. if next O sentence is stating a paragraph
			 *  	*.1. if next T sentence is not starting a paragraph
			 *  		*.1.1 add all remaining T sentences until a next paragraph flag is reached
			 *  	*.2. if next T sentence is starting a paragraph too
			 *  		*.1.1. add this paragraph
			 *  
			 * *. if next O sentence is not starting a paragraph
			 * 		*.1 if next T sentence is not starting a paragraph
			 * 			1.1.1 take one line from translation as well
			 * 		*.2. if next T sentence is starting a paragraph, add remaining sentences from O until a new paragraph
			 * 
			 */
			aCombined.add(senO); iOriginalCursor++;
			aCombined.add(senT); iTranslationCursor++;
			
			boolean stopWhenThereIsAproblem = true;
			
			if (senO.isNextSentenceStartingAParagraph()) {
				if (!senT.isNextSentenceStartingAParagraph()) {
					if (stopWhenThereIsAproblem) {
						System.out.println("     Missing sentences after: " + senO);
						System.out.println("    Missing sentences before: " + arSentenceOriginal.get(iOriginalCursor));
						System.out.println("Check if matches translation: " + arSentenceTranslation.get(iTranslationCursor));
						System.out.println("stop");
					}

					iTranslationCursor = addRemainingSentencesTillParagraph(aCombined, arSentenceTranslation, iTranslationCursor);
				} 
			} else {				
				if (senT.isNextSentenceStartingAParagraph()) {
					if (stopWhenThereIsAproblem) {
						System.out.println("     Missing sentences after: " + senT);
						System.out.println("    Missing sentences before: " + arSentenceTranslation.get(iTranslationCursor));
						System.out.println("Check if matches translation: " + arSentenceOriginal.get(iOriginalCursor));
						System.out.println("stop");
					}
					
					iOriginalCursor = addRemainingSentencesTillParagraph(aCombined, arSentenceOriginal, iOriginalCursor);
				}
				
			}
			
		}
		
		Sentence.outputResultFileSentences(aCombined, fOutCombined);
		

		Sentence senO = arSentenceOriginal.get(arSentenceOriginal.size() - 1);
		Sentence senT = arSentenceTranslation.get(arSentenceTranslation.size() -1);

		Assert.assertEquals("Number of paragraphs should be equal.", 
		                    senO.getLocation_paragraphNumber(), 
		                    senT.getLocation_paragraphNumber());
		
		Assert.assertEquals("Tranlation does not follow in structure the source. " +
				"Line numbers are different", 
				arSentenceOriginal.size(), 
				arSentenceTranslation.size());
	}

	/**
	 * 
	 * 
	 * 
	 * @param aCombined
	 * @param arSentenceTranslation
	 * @param iTranslationCursor
	 * @return
	 */
	public static int addRemainingSentencesTillParagraph(
													ArrayList<Sentence> aCombined,
													ArrayList<Sentence> arSentenceSource,
													int iSourceCursor)
	{
//		Sentence sTest = arSentenceSource.get(iSourceCursor-1);
//		Assert.assertFalse("it should not be the end of the paragraph", sTest.isNextSentenceStartingAParagraph());
		
		for(; iSourceCursor < arSentenceSource.size(); iSourceCursor++) {
			Sentence s2 = arSentenceSource.get(iSourceCursor);
			aCombined.add(s2);
			if (s2.isNextSentenceStartingAParagraph()) {				
				return iSourceCursor + 1;
			}			
		}

		return iSourceCursor;
	}

	/**
	 * @throws IOException
	 */
	@Test public void test_extractListOfSentences() throws IOException
	{
		String sOriginal = FileUtility.readFile(Sentence.TEST_PATH_ORIGINAL_FILE);
		
		ArrayList<Sentence> arSentenceOriginal = Sentence.extractSentences(sOriginal, false);
		Assert.assertTrue("Should contain more then 1 sentences", arSentenceOriginal.size() > 1);
	}
	
	/**
	 * only string mark test
	 * @throws IOException
	 */
	@Test public void test_mark() throws IOException
	{
		String sOriginal = FileUtility.readFile(Sentence.TEST_PATH_ORIGINAL_FILE);

		sOriginal = Sentence.mark(sOriginal);
		Assert.assertTrue("Has to contain: " + Sentence.PARAGRAPH_MARKER, sOriginal.contains(Sentence.PARAGRAPH_MARKER));
		Assert.assertTrue("Has to contain: " + Sentence.SENTENCE_MARKER, sOriginal.contains(Sentence.SENTENCE_MARKER));
	}
	
	
	
}
